/**
* ...
* @author Default
* @version 0.1
*/

package fr.red5.net {
	import flash.events.EventDispatcher;
	import flash.net.NetConnection;
	import flash.net.Responder;
	import flash.utils.setTimeout;

	public class Operation extends EventDispatcher {
		
		public static const TIMEOUT:Number = 12000;
		
		private var connection:Red5Connection;
		private var operationURL:String;
		public var responder:Responder;
		
		
		public function Operation(connection:Red5Connection=null, operationURL:String, args:Array){
			this.operationURL = operationURL;
			this.args = args;
			responder = new Responder(onResult,onFault);
		}
		
		
		private function onFault(fault:Object):void {
			dispatchEvent(new FaultEvent(FaultEvent.Fault, true, true, fault));
		}
		
		private function onResult(result:Object):void {
			dispatchEvent(new ResultEvent(ResultEvent.RESULT, true, true, result));
		}
		
		private function onTimeout():void {
			dispatchEvent(new FaultEvent(FaultEvent.Fault, true, true, {text:"Connection Error : Operation timed out"}));
		}
		
		public function execute():void {
			connection.callMethod(operationURL,responder,args);
			setTimeout(onTimeout, Operation.TIMEOUT);
		}
		
	}
	
}
